package com.yuluke.ylsaudiorecorder.play;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yuluke.ylsaudiorecorder.FileUtils;
import com.yuluke.ylsaudiorecorder.R;

import java.io.File;

public class PlayChooserActivity extends AppCompatActivity {

    private static final String TAG = "YlsRec_ChooserActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_play_chooser);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        init();
    }

    private void init() {
        RecyclerView rv = findViewById(R.id.recycler_view_file_list);
        RecyclerView.LayoutManager layoutManager =
                new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false);
        rv.setLayoutManager(layoutManager);
        MyAdapter adapter = new MyAdapter();
        rv.setAdapter(adapter);
    }

    private void onItemClicked(String fileName, String filePath) {
        Intent intent = new Intent(this, PlayActivity.class);
        intent.putExtra(PlayActivity.EXTRA_FILE_NAME, fileName);
        intent.putExtra(PlayActivity.EXTRA_FILE_PATH, filePath);
        startActivity(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();
        finish();
    }

    class MyAdapter extends RecyclerView.Adapter<MyVH> {

        private final File[] mFileArray;

        public MyAdapter() {
            String dirPath = FileUtils.getPcmFileDir(PlayChooserActivity.this);
            File dir = new File(dirPath);
            mFileArray = dir.listFiles();
            Log.i(TAG, "new MyAdapter, file size:" + mFileArray.length);
        }

        @NonNull
        @Override
        public MyVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.play_chooser_item, parent, false);
            return new MyVH(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyVH holder, int position) {
            File f = mFileArray[position];
            holder.setFileInfo(f.getName(), f.getAbsolutePath());
        }

        @Override
        public int getItemCount() {
            return mFileArray.length;
        }
    }

    class MyVH extends RecyclerView.ViewHolder {

        private final TextView mTextFileName;

        public MyVH(@NonNull View itemView) {
            super(itemView);
            mTextFileName = itemView.findViewById(R.id.text_file_name);
        }

        public void setFileInfo(String name, String path) {
            mTextFileName.setText(name);
            itemView.setOnClickListener( v -> {
                onItemClicked(name, path);
            });
        }
    }
}